package pers.tetris;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;

/**
 * 配置对话框，用于配置model的灵敏度和速度
 * @see TetrisModel#sensitivity
 * @see TetrisModel#speed
 */
public class SettingDialog extends JDialog {
    private TetrisModel model;

    public SettingDialog(Window owner,TetrisModel model) {
        super(owner);

        this.model = model;

        initComponents();
    }

    /**
     * 点击OK按钮
     */
    private void ok(ActionEvent e) {
        model.setSensitivity(range2sensitivity(sliderOfSensitivity.getValue()));
        model.setSpeed(range2speed(sliderOfSpeed.getValue()));
        this.setVisible(false);
    }

    /**
     * 点击Cancel按钮
     */
    private void cancel(ActionEvent e) {
        this.setVisible(false);
    }

    /**
     * 将sensitivity 从 毫秒 转成 0-255的值
     * @param s 毫秒
     * @return 0-255 的值
     */
    private int sensitivity2range(int s){
        return (int) (200-50*Math.log10(s));
    }

    /**
     * 将speed 从 毫秒 转成 0-255的值
     * @param s 毫秒
     * @return 0-255 的值
     */
    private int speed2range(int s){
        return (int) ((-Math.log(s/3000.0)/Math.log(6)+1)*50);
    }

    /**
     * 将sensitivity 从 0-255的值转成 毫秒
     * @param r 0-255 的值
     * @return 毫秒
     */
    private int range2sensitivity(int r){
        return (int) (Math.pow(10.0,1-r/50.0)*1000);
    }

    /**
     * 将speed 从 0-255的值转成 毫秒
     * @param r 0-255 的值
     * @return 毫秒
     */
    private int range2speed(int r){
        return (int) (Math.pow(6,1-r/50.0)*3000);
    }

    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
        if (b){
            this.sliderOfSensitivity.setValue(sensitivity2range(model.getSensitivity()));
            this.sliderOfSpeed.setValue(speed2range(model.getSpeed()));
        }
    }

    /**
     * 对话框的界面设置
     */
    private void initComponents() {
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        label1 = new JLabel();
        sliderOfSensitivity = new JSlider();
        label2 = new JLabel();
        sliderOfSpeed = new JSlider();
        buttonBar = new JPanel();
        okButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        setTitle("\u8bbe\u7f6e");
        setType(Window.Type.UTILITY);
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(Borders.createEmptyBorder("7dlu, 7dlu, 7dlu, 7dlu"));
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(new FormLayout(
                    "default, $lcgap, default",
                    "default, $lgap, default"));

                //---- label1 ----
                label1.setText("\u7075\u654f\u5ea6");
                label1.setLabelFor(sliderOfSensitivity);
                label1.setHorizontalAlignment(SwingConstants.CENTER);
                contentPanel.add(label1, CC.xy(1, 1));

                //---- sliderOfSensitivity ----
                sliderOfSensitivity.setMinorTickSpacing(1);
                contentPanel.add(sliderOfSensitivity, CC.xy(3, 1));

                //---- label2 ----
                label2.setText("\u901f\u5ea6");
                label2.setHorizontalAlignment(SwingConstants.CENTER);
                contentPanel.add(label2, CC.xy(1, 3));
                contentPanel.add(sliderOfSpeed, CC.xy(3, 3));
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(Borders.createEmptyBorder("5dlu, 0dlu, 0dlu, 0dlu"));
                buttonBar.setLayout(new FormLayout(
                    "$lcgap, 47dlu, $lcgap, default, $rgap, $button",
                    "pref"));

                //---- okButton ----
                okButton.setText("OK");
                okButton.addActionListener(e -> ok(e));
                buttonBar.add(okButton, CC.xy(2, 1));

                //---- cancelButton ----
                cancelButton.setText("Cancel");
                cancelButton.addActionListener(e -> cancel(e));
                buttonBar.add(cancelButton, CC.xy(6, 1));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
    }

    private JPanel dialogPane;
    private JPanel contentPanel;
    private JLabel label1;
    private JSlider sliderOfSensitivity;
    private JLabel label2;
    private JSlider sliderOfSpeed;
    private JPanel buttonBar;
    private JButton okButton;
    private JButton cancelButton;
}
